package cc.zy.base.hr.service.impl;

import cc.zy.base.businesses.mapper.BatchMapper;
import cc.zy.base.businesses.mapper.DicMapper;
import cc.zy.base.businesses.mapper.LevelMapper;
import cc.zy.base.businesses.mapper.MajorMapper;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.Lecturer;
import cc.zy.base.hr.entity.OutsideTeacher;
import cc.zy.base.hr.mapper.OutsideTeacherMapper;
import cc.zy.base.hr.service.IOutsideTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * @author 郝秀峰
 * @time 2022/03/08 19:27:30
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class OutsideTeacherServiceImpl extends ServiceImpl<OutsideTeacherMapper, OutsideTeacher> implements IOutsideTeacherService {

    @Resource
    private final OutsideTeacherMapper outsideTeacherMapper;

    @Override
    public IPage<OutsideTeacher> findOutsideTeachers(QueryRequest request, OutsideTeacher outsideTeacher) {

        Page<OutsideTeacher> page = new Page<>(request.getPageNum(), request.getPageSize());

        page.setSearchCount(false);
        page.setTotal(baseMapper.countOutsideTeacherDetail(outsideTeacher));

        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);

        return baseMapper.findOutsideTeacherDetailPage(page, outsideTeacher);
    }

    @Override
    public OutsideTeacher getById(Integer id) {
        return baseMapper.getById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOutsideTeachers(String[] OutsideTeachers) {

        List<String> list = Arrays.asList(OutsideTeachers);

        // 删除用户
        this.removeByIds(list);
    }

    @Override
    public List<OutsideTeacher> findOutsideTeacherListNotPage(OutsideTeacher outsideTeacher) {
        List<OutsideTeacher> outsideTeachersListNotPage = baseMapper.findOutsideTeacherListNotPage(outsideTeacher);

        return outsideTeachersListNotPage;
    }

    @Override
    public List<OutsideTeacher> findOutsideTeachers(OutsideTeacher outsideTeacher) {

        LambdaQueryWrapper<OutsideTeacher> queryWrapper = new LambdaQueryWrapper<>();

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOutsideTeacher(OutsideTeacher outsideTeacher) {

        outsideTeacher.setCreateDate(new Date());
        outsideTeacher.setLastModifyDate(new Date());

        this.save(outsideTeacher);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOutsideTeacher(OutsideTeacher outsideTeacher) {
        this.saveOrUpdate(outsideTeacher);
    }

    @Override
    public Boolean isRepetitive(String realName,String phone) {

        String repetitive = outsideTeacherMapper.isRepetitive(realName,phone);

        Boolean flag = true;

        if (repetitive != null) {
            flag = false;
        }
        return flag;
    }

    @Override
    public void updateOutsideTeacherStatus(Integer outsideTeacherId) {
        outsideTeacherMapper.updateOutsideTeacherStatus(outsideTeacherId);
    }

    /**
     * @Author: PW
     * @Description:
     * @DateTime: 2022/3/8 22:20
     * @Params: [request, outsideTeacher]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<cc.zy.base.hr.entity.OutsideTeacher>
     */

    @Override
    public IPage<Lecturer> listOutsideTeacher(QueryRequest request, Lecturer lecturer) {

        Page<Lecturer> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);

        page.setTotal(outsideTeacherMapper.countOutsideTeacherForSelectDetail(lecturer));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return outsideTeacherMapper.listOutsideTeacher(page, lecturer);
    }

}